import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/assets/css/global.css'
import '@/assets/css/theme/index.css'
import request from "@/utils/request";
import ContactService from './components/ContactService.vue';
import IsLoading from './components/IsLoading.vue';
import VueParticles from 'vue-particles'
import {library} from '@fortawesome/fontawesome-svg-core';
import {faHome, faComments, faInfoCircle, faEnvelope} from '@fortawesome/free-solid-svg-icons';
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome';
import {MessageBox, Message} from 'element-ui';
import Ping from 'ping.js';
import axios from "axios";

// 添加图标到库
library.add(faHome, faComments, faInfoCircle, faEnvelope)

// 注册FontAwesome组件
Vue.component('font-awesome-icon', FontAwesomeIcon)
// 注册全局组件
Vue.component('ContactService', ContactService)
Vue.component('IsLoading', IsLoading)

Vue.config.productionTip = false

Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$message = Message;
Vue.prototype.$request = request
Vue.prototype.$baseUrl = process.env.VUE_APP_BASEURL

Vue.use(ElementUI, {size: "small"})
Vue.use(VueParticles)

function checkNetWork() {
    // let p = new Ping();
    // p.ping("http://tianyou.xyhelp.cn", function (error, data) {
    //     if (error) {
    //         sessionStorage.setItem("netWork", "false")
    //     } else {
    //         sessionStorage.setItem("netWork", "true")
    //     }
    // })
    // p.ping("http://tianyou.xyhelp.cn").then(() => {
    //     sessionStorage.setItem("netWork", "true");
    // }).catch(() => {
    //     sessionStorage.setItem("netWork", "false");
    // });
    // axios.get("http://tianyou.xyhelp.cn").then(res =>{
    //     sessionStorage.setItem("netWork", "true");
    // }).catch(() =>{
    //     sessionStorage.setItem("netWork", "false");
    // })

    // 发起axios请求
    const axiosPromise = axios.get("http://tianyou.xyhelp.cn/prod-api/resource/list/212").then(res => {
        sessionStorage.setItem("netWork", "true");
    }).catch(() => {
        sessionStorage.setItem("netWork", "false");
    })

// 设置一个5秒的定时器
    const timeoutPromise = new Promise((resolve, reject) => {
        setTimeout(() => {
            reject("Timeout");
        }, 5000);
    });

// 使用Promise.race()来同时监听axios请求和定时器
    Promise.race([axiosPromise, timeoutPromise])
        .then(() => {
            // 请求在5秒内完成
            console.log("Request completed within 5 seconds");
            sessionStorage.setItem("netWork", "true");
            return
        })
        .catch(error => {
            // 请求超时，或者出现其他错误
            console.error(error);
            sessionStorage.setItem("netWork", "false");
        });
}

checkNetWork()

window.onunhandledrejection = function (event) {
    console.error('未捕获的Promise拒绝:', event.reason);
};

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')